1
|
|
|
import fs from 'fs-extra'; |
2
|
|
|
import API from 'base-api-client'; |
3
|
|
|
|
4
|
|
|
export default class HerokuAPI extends API { |
5
|
|
|
constructor(name, apiKey) { |
6
|
|
|
super('https://api.heroku.com/'); |
7
|
|
|
this.name = name; |
8
|
|
|
this.apiKey = apiKey; |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
getHeaders() { |
12
|
|
|
return { |
13
|
|
|
accept : 'application/vnd.heroku+json; version=3', |
14
|
|
|
authorization : `Bearer ${this.apiKey}` |
15
|
|
|
}; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
async createBuild(src, version) { |
19
|
|
|
const build = await this.post( |
20
|
|
|
`/apps/${this.name}/builds`, |
21
|
|
|
{ 'source_blob': { url: src, version } } |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
return { |
25
|
|
|
id : build.id, |
26
|
|
|
source : build.source_blob, |
27
|
|
|
status : build.status |
28
|
|
|
}; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
async createSource(src, version) { |
32
|
|
|
const source = await this.post( |
33
|
|
|
`/apps/${this.name}/sources`, |
34
|
|
|
{ 'source_blob': { url: src, version } } |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
return { |
38
|
|
|
get : source.source_blob.get_url, |
39
|
|
|
put : source.source_blob.put_url |
40
|
|
|
}; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
async upload(url, file) { |
44
|
|
|
const info = await fs.stat(file); |
45
|
|
|
const readmeStream = fs.createReadStream(file); |
46
|
|
|
|
47
|
|
|
readmeStream.on('error', console.error); |
48
|
|
|
|
49
|
|
|
await this.put( |
50
|
|
|
url, |
51
|
|
|
readmeStream, |
52
|
|
|
{ headers : { |
53
|
|
|
'Content-Type' : '', |
54
|
|
|
'Content-Length' : info.size |
55
|
|
|
} } |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
async test() { |
60
|
|
|
const app = await this.get(`apps/${this.name}`); |
61
|
|
|
|
62
|
|
|
return app.id; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|